package com.vincce.sport.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.entity.City;
import com.vincce.sport.entity.Page;
import com.vincce.sport.mapper.CityMapper;
import com.vincce.sport.mapper.ZoneMapper;

@Controller
@RequestMapping("/city")
public class CityController extends BaseController{
	@Resource
	private CityMapper cityMapper;
	
	@Resource
	private ZoneMapper zoneMapper;
	
	/**
	 * 分页查询
	 * @param model
	 * @param page
	 * @param city
	 * @return
	 */
	@RequestMapping("/cityList")
	public String queryCityByPage(Model model,Page<City> page,City city){
		Map<String, Object> filter = new HashMap<String, Object>();
		if(city.getName() != null && !city.getName().equals("")){
	            filter.put("name", city.getName());
	    }
		if(city.getZoneId() != null && !city.getZoneId().equals("")){
            filter.put("zoneId", city.getZoneId());
        }
		page.setFilter(filter);
		page.setTotal(cityMapper.queryByCityCount(page.getFilter()));
		page.setResult(cityMapper.queryByCityPage(page));
		
		model.addAttribute("zoneList",zoneMapper.queryByZoneAll());
		model.addAttribute("city",city);
		model.addAttribute("page",page);
		return "/backstage/cityList";
	}
	
	
	/**
	 * 新增
	 * @param model
	 * @param city
	 * @return
	 */
	@RequestMapping("/addCity")
	@ResponseBody
	public String insertCity(Model model,City city){
		Boolean flag=false;
		city.setCreateUser(getCurrentUser().getId().intValue());
		city.setCreateTime(new Date());
		Long count=cityMapper.insertCity(city);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	
	/**
	 * 修改
	 * @param model
	 * @param city
	 * @return
	 */
	@RequestMapping("/updateCity")
	@ResponseBody
	public String updateZone(Model model,City city){
		Boolean flag=false;
		
		Long count=cityMapper.updateCity(city);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	/**
	 * 根据ID删除
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(Model model,Integer id){
		Boolean flag=false;
		Integer count=cityMapper.deleteById(id);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	@RequestMapping("/queryCityByZoneId")
	@ResponseBody
	public String queryCityByZoneId(Model model,Integer zoneId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("zoneId", zoneId);
		List<City> list=cityMapper.queryCityByZoneId(map);
        return JSON.toJSONString(list);
	}
	
	
	@RequestMapping("/checkUnique")
	@ResponseBody
	public String checkUnique(City city) throws IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		if(!CheckUtil.isEmpty(city.getName())){
			map.put("name", city.getName());
		}
		
		if(!CheckUtil.isEmpty(city.getId())){
			map.put("id", city.getId());
		}
		
		City cityListJson = cityMapper.checkUniqueCity(map);
		boolean flag = false;
		if(cityListJson == null || cityListJson.equals("") || cityListJson.equals("{}")){
			flag = true;
		}
		return JSON.toJSONString(flag);
	}  
 
}
